package com.netnovelstore.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;


@Data
@Slf4j
@Component
public class FileManager {

    @Value("${web.resource-path}")
    private String parentsFolderPath;
    @Value("${web.book-path}")
    private String parentsFolderBookPath;
    @Value("${web.profile-path}")
    private String parentsFolderProFilePath;
    @Value("${web.covers-path}")
    private String parentsFolderCoversPath;


    private Resolver resolver;

    @Autowired
    public FileManager(Resolver resolver) {
        this.resolver = resolver;
    }

    /**
     * @Author maotao
     * @Description 写入时间戳文件
     * @Date 22:03 2022/10/4
     * @Param [path, multipartFile]
     * @return java.lang.String "newFile.jpeg"
     **/
    public String writeFile(String path,MultipartFile multipartFile) throws IOException {


        String fileFullName = multipartFile.getOriginalFilename();

        //获取文件后缀
        String fileSubffix = fileFullName.substring(fileFullName.lastIndexOf(".") + 1);

        //获得毫秒时间戳
        String fileName = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());

        String fileFullPath = String.format("%s/%s.%s", path, fileName, fileSubffix);

        log.info("[writeFile] =>> " + fileFullPath);
        File file = new File(this.parentsFolderProFilePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        multipartFile.transferTo(new File(fileFullPath));

        return String.format("%s.%s", fileName, fileSubffix);

    }
    /**
     * @Author maotao
     * @Description 传入文件, 自动更名生成时间戳并存入指定目录
     * @Date 10:47 2022/10/3
     * @Param [multipartFile] "oldFile.jpeg"
     * @return java.lang.String "newFile.jpeg"
     **/
    public String writeProfile(MultipartFile multipartFile) throws IOException {
        return this.writeFile(this.parentsFolderProFilePath, multipartFile);
    }




    /**
     * @return boolean 删除成功与否
     * @Author maotao
     * @Description 传入图片全名, 删除指定目录下的指定图片
     * @Date 10:50 2022/10/3
     * @Param [fileFullName] "maotao.jpeg"
     **/
    public boolean deleteProfile(String fileFullName){
        try{
            String fileFullPath = String.format("%s/%s", this.parentsFolderPath, fileFullName);
            log.info("[deleteFile] =>> " + fileFullPath);
            File file = new File(fileFullPath);
            return file.delete();
        }catch (Exception e){
            return false;
        }
    }

    /**
     * @return java.lang.String
     * @Author maotao
     * @Description 按照书名和章节, 返回章节正文
     * @Date 10:52 2022/10/3
     * @Param [bookName, number]
     **/
    public String readFile_txt(String bookPath, Integer number) throws IOException {
        return novelReader.readContent(parentsFolderBookPath, bookPath, number);
    }

    /**
     * @return java.lang.Integer
     * @Author maotao
     * @Description 利用二分搜索获取磁盘章节数量
     * @Date 10:52 2022/10/3
     * @Param [bookName]
     **/
    public Integer getChapterTotal(String bookName) {
        return novelReader.getChapterTotal(parentsFolderBookPath, bookName);
    }

    /**
     * @return void
     * @Author maotao
     * @Description
         * 项目启动的时候执行该方法
         * 异步解析未拆封的书籍, 按正文拆分成若干个章节
     * @Date 10:52 2022/10/2
     * @Param null
     **/
    public void resolverNovel() {
        try {
            resolver.run(this.parentsFolderBookPath);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
